package org.ping.qq.Login;

import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.PageFans;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.PageFansBean;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.javabeans.weibo.Company;
import com.qq.connect.oauth.Oauth;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ping.Tool.LogUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Date: 12-12-4
 * Time: 下午4:36
 */
@WebServlet("/AfterLoginRedirectServlet")
public class AfterLoginRedirectServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    //我只要获取到用户的头像地址和昵称//登录时间，登录ip。。。
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=utf-8");
        
        String accessToken   = null,
                openID        = null;
        String userName = "";
        String userImage = "";
        
        /*String code = request.getParameter("code");
        String state = request.getParameter("state");
        System.out.println("code："+code);
        System.out.println("state"+state);
        LogUtil.getLogUtil(this).showlog("code------->:"+code);
        LogUtil.getLogUtil(this).showlog("state------>："+state);
        
        LogUtil.getLogUtil(this).showlog("用户名："+userName);*/
      
        try {
        	
        	//获取到包含信息的accessTokenObj
            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
            
            if (accessTokenObj.getAccessToken().equals("")) {
            	//我们的网站被CSRF攻击了或者用户取消了授权
            	//做一些数据统计工作
            	LogUtil.getLogUtil(this).showlog("没有获取到响应参数");
            } else {
            	//获取到详细的accessToken
                accessToken = accessTokenObj.getAccessToken();
                LogUtil.getLogUtil(this).showlog("accessToken:"+accessToken);
                
                // 利用获取到的accessToken 去获取当前用的openid -------- start
                OpenID openIDObj =  new OpenID(accessToken);
                openID = openIDObj.getUserOpenID();
                
                //利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息
                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
                
                if (userInfoBean.getRet() == 0) {
                	userName = userInfoBean.getNickname();
                	userImage = userInfoBean.getAvatar().getAvatarURL100();                
                } else {
                	LogUtil.getLogUtil(this).showlog("没有获取到信息！"+userInfoBean.getMsg());
                }
            }
        } catch (QQConnectException e) {
        	
        }
        //添加到客户端
        Cookie user = new Cookie("user", userName);
        Cookie image = new Cookie("image", userImage);
        user.setMaxAge(1000);
        image.setMaxAge(1000);
        response.addCookie(user);
        response.addCookie(image);
        
        response.sendRedirect("index.jsp");
//        try {
//			request.getRequestDispatcher("http://ping.中国").forward(request, response);
//		} catch (ServletException e) {
//			e.printStackTrace();
//		}
        //如果用户名不等于null那么就存数据库
        if(userName!=null) {
        	SetDate set = SetDate.getDate();
            if(set.selectUser(openID)) {
            	set.addUser(userName, openID);
            }
        }
        
        //LogUtil.getLogUtil(this).showlog("用户名："+userName);
    }
}
